package com.example.elmmerchants.First_Page_Fragment;

import static android.content.ContentValues.TAG;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.elmmerchants.Check_Order_Service;
import com.example.elmmerchants.R;
import com.google.android.material.bottomnavigation.BottomNavigationView;

public class First_Page extends AppCompatActivity {

    BottomNavigationView page_btn;
    String email;
    boolean is_open_auto_get_order = false, is_loading_data = false, is_order_data = false;
    private ActivityResultLauncher<String> requestPermission;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_page);
        requestPermission = registerForActivityResult(new ActivityResultContracts.RequestPermission(), result -> {
            // 权限申请结果
            Log.d(TAG, "权限申请结果: " + result);
        });

        email = getIntent().getStringExtra("email");
        is_open_auto_get_order = getIntent().getBooleanExtra("is_open_auto_get_order", false);

        //Re_Loading_Order_Data_Thread reLoadingOrderDataThread = new Re_Loading_Order_Data_Thread();
        //reLoadingOrderDataThread.start();
        // 检查是否已经获取到定位权限
        if (checkSelfPermission(First_Page.this, Manifest.permission.FOREGROUND_SERVICE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            // 获取到权限
            Log.d(TAG, "onResume: 已获取到权限");
            Intent intent = new Intent(First_Page.this, Check_Order_Service.class);
            intent.putExtra("email", email);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                startForegroundService(intent);
            }
            else {
                startService(intent);
            }
        } else {
            // 请求定位权限
            requestPermission.launch(Manifest.permission.FOREGROUND_SERVICE_LOCATION);
        }

        set_page(new Order_Page(email));

        page_btn = findViewById(R.id.page_btn);
        page_btn.setItemIconTintList(null);
        page_btn.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                int itemId = item.getItemId();

                if (itemId == R.id.order_page) {
                    set_page(new Order_Page(email));
                }
                else if (itemId == R.id.shop_page) {
                    set_page(new Shop_Page(email));
                }
                else if (itemId == R.id.message_page) {
                    set_page(new Message_Page(email));
                }
                else if (itemId == R.id.setting_page) {
                    set_page(new Setting_Page(email));
                }
                /*else if (itemId == R.id.shop_page) {
                    set_page(new Shop_Page(phone_number, shop_name));
                }
                else if (itemId == R.id.setting_page) {
                    set_page(new Setting_Page());
                }*/

                return true;
            }
        });
    }

    private int checkSelfPermission(First_Page firstPage, String accessFineLocation) {
        return 0;
    }

    public void set_page(Fragment fragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.set_page, fragment).commit();
    }
}